package com.softer.chengxin.score.dao.provider;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.score.entity.ScoreDTO;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * @author Gong Zhiyang
 * @date 2018/10/11
 */
public class ScoreDetailDaoProvider {

    /**
     * 查询录入信息的数量
     *
     * @param map 数据
     * @return sql语句
     */
    public String queryNum(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        ScoreDTO scoreDTO = (ScoreDTO) map.get("scoreDTO");
        String states = (String) map.get("states");
        sql.append("SELECT count(*) FROM score_detail a");
        sql.append(" LEFT JOIN company_project b ON b.id = a.project_id WHERE IF(a.state=0,a.adder=#{loginName},1=1) AND a.corp_id=#{scoreDTO.corpId}");
        sql.append(" and a.info_type=#{scoreDTO.infoType}");
        commonSql(sql, scoreDTO,states);
        return sql.toString();
    }

    /**
     * 查询录入信息列表
     *
     * @param map 数据
     * @return sql语句
     */
    public String queryList(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        Page page = (Page) map.get("page");
        ScoreDTO scoreDTO = (ScoreDTO) map.get("scoreDTO");
        String states = (String) map.get("states");
        sql.append("SELECT a.id,a.reason,b.project_name AS projectName,");
        sql.append("a.score,DATE_FORMAT(a.gmt_create, '%Y-%m-%d') AS gmtCreate,a.state FROM score_detail a");
        sql.append(" LEFT JOIN company_project b ON b.id = a.project_id WHERE IF(a.state=0,a.adder=#{loginName},1=1) AND a.corp_id=#{scoreDTO.corpId}");
        sql.append(" and a.info_type=#{scoreDTO.infoType}");
        commonSql(sql, scoreDTO,states);
        sql.append(" LIMIT ");
        sql.append(page.getBeginIndex()).append(",").append(page.getLimit());
        return sql.toString();
    }

    private void commonSql(StringBuffer sql, ScoreDTO scoreDTO,String states) {
        if (!Common.ZERO.equals(scoreDTO.getProjectId())) {
            sql.append(" and  a.project_id=#{scoreDTO.projectId}");
        }
        if (StringUtils.isNotBlank(scoreDTO.getName())) {
            sql.append(" AND INSTR(b.project_name,#{scoreDTO.name})>0");
        }
        if (scoreDTO.getState() != null) {
            if (scoreDTO.getState().equals(NumberEnum.ONE.getNum())) {
                sql.append(" AND a.state in (1,9)");
            } else if (scoreDTO.getState().equals(NumberEnum.FOUR.getNum())) {
                sql.append(" AND a.state in (4,10)");
            } else {
                sql.append(" AND FIND_IN_SET(a.state,#{states}) ");
            }
        }
        if (null != states) {
            if (states.equals(NumberEnum.ONE.getNum())) {
                sql.append(" AND a.state in (1,9)");
            } else if (states.equals(NumberEnum.FOUR.getNum())) {
                sql.append(" AND a.state in (4,10)");
            } else {
                sql.append(" AND FIND_IN_SET(a.state,#{states}) ");
            }
        }
        if (StringUtils.isNotBlank(scoreDTO.getReason())) {
            sql.append(" AND INSTR(a.reason,#{scoreDTO.reason})>0");
        }
    }

    /**
     * 获取最近的信息（用于首页展示）
     * @param map 参数
     * @return sql
     */
    public String getCurrentMsg(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        String projectId = (String) map.get("projectId");
        sql.append("SELECT a.id,a.reason,b.project_name AS projectName,a.score,a.approver,");
        sql.append("DATE_FORMAT(a.effective_begin_time, '%Y-%m-%d') as effectiveBeginTime,a.info_type as infoType,");
        sql.append("DATE_FORMAT(a.effective_end_time, '%Y-%m-%d') as effectiveEndTime FROM score_detail a");
        sql.append(" LEFT JOIN company_project b ON b.id = a.project_id WHERE a.corp_id=#{id}");
        sql.append(" and a.state IN(2,3,5)");
        if (!Common.ZERO.equals(projectId)) {
            sql.append(" and  a.project_id=#{projectId}");
        }
        sql.append(" ORDER BY effectiveBeginTime DESC LIMIT 5");
        return sql.toString();
    }
}
